<?php
namespace app\horoscope\controller;

use \app\check\controller\Check;
use \app\astro\controller\Data;
use \think\Db;

class Jq extends Check
{
    public function __construct()
    {
        parent::filter_xss();
        parent::__construct();// 接口调用验证
    }

    public function index()
    {
/*
dim $md
'确定节气 yz 月支  起运基数 $qyjs
*/
        $b[31] = "鼠 ";
        $b[32] = "牛 ";
        $b[33] = "虎 ";
        $b[34] = "兔 ";
        $b[35] = "龙 ";
        $b[36] = "蛇 ";
        $b[37] = "马 ";
        $b[38] = "羊 ";
        $b[39] = "猴 ";
        $b[40] = "鸡 ";
        $b[41] = "狗 ";
        $b[30] = "猪 ";

        $c[31] = "水 ";
        $c[32] = "水 ";
        $c[33] = "木 ";
        $c[34] = "木 ";
        $c[35] = "木 ";
        $c[36] = "火 ";
        $c[37] = "火 ";
        $c[38] = "火 ";
        $c[39] = "金 ";
        $c[40] = "金 ";
        $c[41] = "金 ";
        $c[30] = "水 ";

#天干
        $d[21] = "a";
        $d[22] = "a";
        $d[23] = "b";
        $d[24] = "b";
        $d[25] = "c";
        $d[26] = "c";
        $d[27] = "d";
        $d[28] = "d";
        $d[29] = "e";
        $d[20] = "e";

        $da[21] = "1";
        $da[22] = "0";
        $da[23] = "1";
        $da[24] = "0";
        $da[25] = "1";
        $da[26] = "0";
        $da[27] = "1";
        $da[28] = "0";
        $da[29] = "1";
        $da[20] = "0";

#天干合化
        $e[21] = "土";
        $e[22] = "金";
        $e[23] = "水";
        $e[24] = "木";
        $e[25] = "火";
        $e[26] = "土";
        $e[27] = "金";
        $e[28] = "水";
        $e[29] = "木";
        $e[20] = "火";

        $a[21] = "甲";
        $a[22] = "乙";
        $a[23] = "丙";
        $a[24] = "丁";
        $a[25] = "戊";
        $a[26] = "己";
        $a[27] = "庚";
        $a[28] = "辛";
        $a[29] = "壬";
        $a[20] = "癸";

        $a[31] = "子 ";
        $a[32] = "丑 ";
        $a[33] = "寅 ";
        $a[34] = "卯 ";
        $a[35] = "辰 ";
        $a[36] = "巳 ";
        $a[37] = "午 ";
        $a[38] = "未 ";
        $a[39] = "申 ";
        $a[40] = "酉 ";
        $a[41] = "戌 ";
        $a[30] = "亥 ";

#十神名称

        $a[1] = "比肩";
        $a[2] = "劫财";
        $a[3] = "食神";
        $a[4] = "伤官";
        $a[5] = "偏财";
        $a[6] = "正财";
        $a[7] = "七杀";
        $a[8] = "正官";
        $a[9] = "偏印";
        $a[0] = "正印";

        $Data = new Data();
        $rid = Input::get('id','0');

        $userinfo = $Data->get_birth_data($rid);

        $year = $userinfo['year'];
        $month = $userinfo['month'];
        $day = $userinfo['day'];
        $t_ime = $userinfo['hour'];

        $yearday = '';
#确定节气 yz 月支  起运基数 qyjs
        $md = $month * 100 + $day;
        if ($md >= 204 and $md <= 305) {
            $mz = 3;
            $qyjs = (($month - 2) * 30 + $day - 4) / 3;
        }
        if ($md >= 306 and $md <= 404) {
            $mz = 4;
            $qyjs = (($month - 3) * 30 + $day - 6) / 3;
        }

        if ($md >= 405 and $md <= 504) {
            $mz = 5;
            $qyjs = (($month - 4) * 30 + $day - 5) / 3;
        }

        if ($md >= 505 and $md <= 605) {
            $mz = 6;
            $qyjs = (($month - 5) * 30 + $day - 5) / 3;
        }

        if ($md >= 606 and $md <= 706) {
            $mz = 7;
            $qyjs = (($month - 6) * 30 + $day - 6) / 3;
        }

        if ($md >= 707 and $md <= 807) {
            $mz = 8;
            $qyjs = (($month - 7) * 30 + $day - 7) / 3;
        }

        if ($md >= 808 and $md <= 907) {
            $mz = 9;
            $qyjs = (($month - 8) * 30 + $day - 8) / 3;
        }

        if ($md >= 908 and $md <= 1007) {
            $mz = 10;
            $qyjs = (($month - 9) * 30 + $day - 8) / 3;
        }

        if ($md >= 1008 and $md <= 1106) {
            $mz = 11;
            $qyjs = (($month - 10) * 30 + $day - 8) / 3;
        }

        if ($md >= 1107 and $md <= 1207) {
            $mz = 0;
            $qyjs = (($month - 11) * 30 + $day - 7) / 3;
        }

        if ($md >= 1208 and $md <= 1231) {
            $mz = 1;
            $qyjs = ($day - 8) / 3;
        }

        if ($md >= 101 and $md <= 105) {
            $mz = 1;
            $qyjs = (30 + $day - 4) / 3;
        }

        if ($md >= 106 and $md <= 203) {
            $mz = 2;
            $qyjs = (($month - 1) * 30 + $day - 6) / 3;
        }


###############
#确定年干和年支 yg 年干 yz 年支
        if ($md >= 204 and $md <= 1231) {
            $yg = ($year - 3) % 10;
            $yz = ($year - 3) % 12;
        }
        if ($md >= 101 and $md <= 203) {
            $yg = ($year - 4) % 10;
            $yz = ($year - 4) % 12;
        }
###############
#确定月干 mg 月干
        If ($mz > 2 And $mz <= 11) {

            $mg = ($yg * 2 + $mz + 8) % 10;
        } else {
            $mg = ($yg * 2 + $mz) % 10;
        }
###############
#从公元0年到目前年份的天数 yearlast
#计算某月某日与当年1月0日的时间差（以日为单位）yearday
        $yearlast = ($year - 1) * 5 + ($year - 1) / 4 - ($year - 1) / 100 + ($year - 1) / 400;

        for ($i = 1; $i < $month; $i++) {
            switch ($i) {
                case 1:
                    $yearday = $yearday + 31;
                    break;
                case 3:
                    $yearday = $yearday + 31;
                    break;
                case 5:
                    $yearday = $yearday + 31;
                    break;
                case 7:
                    $yearday = $yearday + 31;
                    break;
                case 8:
                    $yearday = $yearday + 31;
                    break;
                case 10:
                    $yearday = $yearday + 31;
                    break;
                case 12:
                    $yearday = $yearday + 31;
                    break;
                case 4:
                    $yearday = $yearday + 30;
                    break;
                case 6:
                    $yearday = $yearday + 30;
                    break;
                case 9:
                    $yearday = $yearday + 30;
                    break;
                case 11:
                    $yearday = $yearday + 30;
                    break;
                case 2:
                    if ($year % 4 == 0 && $year % 100 <> 0 || $year % 400 == 0) {
                        $yearday = $yearday + 29;
                    } else {
                        $yearday = $yearday + 28;
                    }
                    break;
            }
        }
        $yearday = $yearday + $day;
#计算日的六十甲子数 day60
        $day60 = ($yearlast + $yearday + 6015) % 60;
#确定 日干 dg  日支  dz
        $dg = $day60 % 10;
        $dz = $day60 % 12;
#确定 时干 tg 时支 tz
        $tz = ($t_ime + 3) / 2 % 12;
#tg = (dg * 2 + tz + 8) Mod 10
        if ($tz == 0) {
            $tg = ($dg * 2 + $tz) % 10;
        } else {
            $tg = ($dg * 2 + $tz + 8) % 10;
        }
/*#到此，已经完成把 年月日时 转换成为 生辰八字的任务
'确定各地支所纳天干
'年支纳干 yzg 月支纳干 mzg  日支纳干 dzg 时支纳干 tzg
'年支纳干 yzg*/

        switch ($yz) {
            case 1:
                $yzg =0;
                break;
            case 2:
                $yzg =6;
                break;
            case 8:
                $yzg =6;
                break;
            case 3:
                $yzg =1;
                break;
            case 4:
                $yzg =2;
                break;
            case 5:
                $yzg =5;
                break;
            case 11:
                $yzg =5;
                break;
            case 6:
                $yzg =3;
                break;
            case 7:
                $yzg =4;
                break;
            case 9:
                $yzg =7;
                break;
            case 10:
                $yzg =8;
                break;
            case 0:
                $yzg =9;
                break;
        }
#月支纳干 mzg
        switch ($mz) {
            case 1:
                $mzg =0;
                break;
            case 2:
                $mzg =6;
                break;
            case 8:
                $mzg =6;
                break;
            case 3:
                $mzg =1;
                break;
            case 4:
                $mzg =2;
                break;
            case 5:
                $mzg =5;
                break;
            case 11:
                $mzg =5;
                break;
            case 6:
                $mzg =3;
                break;
            case 7:
                $mzg =4;
                break;
            case 9:
                $mzg =7;
                break;
            case 10:
                $mzg =8;
                break;
            case 0:
                $mzg =9;
                break;
        }
#'日支纳干 dzg
        switch ($dz) {
            case 1:
                $dzg =0;
                break;
            case 2:
                $dzg =6;
                break;
            case 8:
                $dzg =6;
                break;
            case 3:
                $dzg =1;
                break;
            case 4:
                $dzg =2;
                break;
            case 5:
                $dzg =5;
                break;
            case 11:
                $dzg =5;
                break;
            case 6:
                $dzg =3;
                break;
            case 7:
                $dzg =4;
                break;
            case 9:
                $dzg =7;
                break;
            case 10:
                $dzg =8;
                break;
            case 0:
                $dzg =9;
                break;
        }
#'时支纳干 tzg
        switch ($tz) {
            case 1:
                $tzg =0;
                break;
            case 2:
                $tzg =6;
                break;
            case 8:
                $tzg =6;
                break;
            case 3:
                $tzg =1;
                break;
            case 4:
                $tzg =2;
                break;
            case 5:
                $tzg =5;
                break;
            case 11:
                $tzg =5;
                break;
            case 6:
                $tzg =3;
                break;
            case 7:
                $tzg =4;
                break;
            case 9:
                $tzg =7;
                break;
            case 10:
                $tzg =8;
                break;
            case 0:
                $tzg =9;
                break;
        }
#到此，完成各地支所纳天干的确定任务

#确定各支对应的十神

#年干十神 ygs
        $ygs = (($yg + 11 - $dg) + (($dg + 1) % 2) * (($yg + 10 - $dg) % 2) * 2) % 10;

#月干十神 $mgs
        $mgs = (($mg + 11 - $dg) + (($dg + 1) % 2) * (($mg + 10 - $dg) % 2) * 2) % 10;

#时干十神 $dgs
        $tgs = (($tg + 11 - $dg) + (($dg + 1) % 2) * (($tg + 10 - $dg) % 2) * 2) % 10;

#年支十神 yzs
        $yzs = (($yzg + 11 - $dg) + (($dg + 1) % 2) * (($yzg + 10 - $dg) % 2) * 2) % 10;

#月支十神 mzs;
        $mzs = (($mzg + 11 - $dg) + (($dg + 1) % 2) * (($mzg + 10 - $dg) % 2) * 2) % 10;

#日支十神 dzs
        $dzs = (($dzg + 11 - $dg) + (($dg + 1) % 2) * (($dzg + 10 - $dg) % 2) * 2) % 10;

#时支十神 tzs
        $tzs = (($tzg + 11 - $dg) + (($dg + 1) % 2) * (($tzg + 10 - $dg) % 2) * 2) % 10;

#到此，完成年月日时，各干支十神的确定

        echo "<br/>出生时间：";
        echo "$userinfo[year]-$userinfo[month]-$userinfo[day] $userinfo[hour]:$userinfo[minute]";

        echo "<br/>八字：";

        echo $a[20+$yg].$a[30+$yz];
        echo $a[20+$mg].$a[30+$mz];
        echo $a[20+$dg].$a[30+$dz];
        echo $a[20+$tg].$a[30+$tz];

        echo "<br/>生肖：";
        echo $b[30+$yz];


        echo "<br/>十神：";
        echo "$a[$ygs] ";
        echo "$a[$mgs] ";
        echo "日主 ";
        echo "$a[$tgs] ";
//        echo "<br/>支十神：";
//        echo "$a[$yzs] ";
//        echo "$a[$mzs] ";
//        echo "$a[$dzs] ";
//        echo "$a[$tzs] ";


        echo "<br/><br/>";
        for($i=1;$i<9;$i++) {
            $sx = (($mg+10)-$i)%10;
            $xy = (($sx + 11 - $dg) + (($dg + 1) % 2) * (($sx + 10 - $dg) % 2) * 2) % 10;
            echo $a[$xy]." ";
        }
        echo "<br/>";
        for($i=1;$i<9;$i++) {
            echo  $a[20+((($mg+10)-$i)%10)].$a[30+((($mz+12)-$i)%12)].' ';
        }
        echo "<br/>";
        $qyjsxy= floor($qyjs);
        for($i=1;$i<9;$i++) {

            echo $qyjsxy.'岁 ';
            $qyjsxy=($qyjsxy+10);
        }
        echo "<br/>";
        echo $year+floor($qyjs)+0,' ';
        echo $year+floor($qyjs)+10,' ';
        echo $year+floor($qyjs)+20,' ';
        echo $year+floor($qyjs)+30,' ';
        echo $year+floor($qyjs)+40,' ';
        echo $year+floor($qyjs)+50,' ';
        echo $year+floor($qyjs)+60,' ';
        echo $year+floor($qyjs)+70,' ';

        dump($qyjs);




    }

}